﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ALH.DB.Tables;
using SQLite.CodeFirst;

namespace ALH.DB
{
    public class CustomHistory : IHistory
    {
        public int Id { get; set; }
        public string Hash { get; set; }
        public string Context { get; set; }
        public DateTime CreateDate { get; set; }
    }

    public class AHLDBContext: DbContext
    {
        public virtual DbSet<DataSources> DataSources { get; set; }

        public AHLDBContext() : base("default")
        {
            Configuration.ProxyCreationEnabled = true;
            Configuration.LazyLoadingEnabled = true;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var sqliteConnectionInitializer = new MyDbContextInitializer(modelBuilder);
            
            Database.SetInitializer(sqliteConnectionInitializer);
        }
    }

    public class MyDbContextInitializer : SqliteDropCreateDatabaseWhenModelChanges<AHLDBContext>
    {
        public MyDbContextInitializer(DbModelBuilder modelBuilder)
            : base(modelBuilder, typeof(CustomHistory)) { }

        protected override void Seed(AHLDBContext context)
        {
            context.DataSources.Add(new DataSources
            {
                Name = "Richard Niemand",
                Created = DateTime.Now
            });

            base.Seed(context);
        }
    }
}
